<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>容平卡</title>
    #include("common/header.html")
    <link rel="stylesheet" href="/admin/css/table.css"/>
</head>

<body>
<div class="admin-main">

    <blockquote class="layui-elem-quote">
        <a href="javascript:;" class="layui-btn layui-btn-small" id="add">
            <i class="layui-icon">&#xe608;</i> 添加容平卡
        </a>
    </blockquote>
    <fieldset class="layui-elem-field">
        <legend>容平卡</legend>
        <div class="layui-field-box layui-form">
            <table class="layui-table admin-table">
                <thead>
                <tr>
                    <th>额度</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody id="pager">
                </tbody>
            </table>
        </div>
    </fieldset>
</div>
</body>

<script type="text/javascript">

    $(function () {
        pageList();
    });

    function pageList() {

        $.ajax({
            url: "/admin/card/",
            type: "get",
            dataType: "json",
            data: {"type": 0},
            success: function (r) {

                console.log(r);

                if (r.code == -1) {
                    return;
                }

                $("#pager").html("");

                var str = "";

                $.each(r.data, function (n, value) {
                    str += '<tr>';
                    str += '<td>' + value.integral + '</td>';
                    str += '<td>';
                    str += '<a onclick="edit(' + value.id + ')" class="layui-btn layui-btn-normal layui-btn-mini">修改</a>';
                    str += '<a onclick="del(' + value.id + ')" class="layui-btn layui-btn-danger layui-btn-mini">删除</a>';
                    str += '</td>';
                    str += '</tr>';
                });

                $("#pager").html(str);
            }
        });
    }


    var layer;
    layui.use('layer', function () {
        layer = layui.layer;
    });

    $("#add").on("click", function () {
        layer.prompt({title: '请输入积分额度', formType: 2}, function (text, index) {
            $.ajax({
                url: "/admin/card/",
                type: "post",
                dataType: "json",
                data: {"integral": text,"type":0},
                success: function (r) {
                    console.log(r);

                    if (r.code == -1) {
                        layer.msg(r.msg);
                        return;
                    }

                    layer.msg('添加成功!', {time: 1000}, function () {
                        //关闭后的操作
                        layer.close(index);
                        location.reload();
                    });
                }
            });
        });
    })

    function edit(id) {

        layer.prompt({title: '请输入修改的积分额度', formType: 1}, function (text, index) {
            $.ajax({
                url: "/admin/card/",
                type: "put",
                dataType: "json",
                data: {"id": id, "integral": text},
                success: function (r) {
                    console.log(r);

                    if (r.code == -1) {
                        layer.msg(r.msg);
                        return;
                    }

                    layer.msg('修改成功!', {time: 1000}, function () {
                        //关闭后的操作
                        layer.close(index);
                        location.reload();
                    });
                }
            });
        });
    }

    function del(id) {

        //询问框
        layer.confirm('是否删除?', {
            btn: ['确定', '取消'] //按钮
        }, function () {
            $.ajax({
                url: "/admin/card/" + id,
                type: "delete",
                dataType: "json",
                success: function (r) {
                    console.log(r);

                    if (r.code == -1) {
                        layer.msg(r.msg);
                        return;
                    }

                    layer.msg('删除成功!', {time: 1000}, function () {
                        //关闭后的操作
                        location.reload();
                    });
                }
            });
        });
    }

</script>

</html>